﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using CubicFWNet.Mvc.Models;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class CustomerController : ControllerBase
    {
        private CustomerApp _customerApp;
        private CustomerProductApp _customerProductApp;
        private Guid salersID = new Guid("4cf6a365-e372-42ce-bd05-ceec18b294a4");
        public CustomerController()
        {
            _customerApp = new CustomerApp();
            _customerProductApp = new CustomerProductApp();

        }

        /// <summary>
        /// 查询客户
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int page = 1, string name = "")
        {
            ViewBag.Name = name;
            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            Guid salerID = operatorModel.UserId;
            if (operatorModel.UserName == "admin")
            {
                salerID = new Guid();
            }
            IPagedList<Customer> cusList = _customerApp.GetList(name, salerID, 1).ToPagedList(page, 10);
            return View(cusList);
        }

        /// <summary>
        /// 客户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List(int page = 1, string name = "")
        {
            ViewBag.Name = name;
            Guid salerID = operatorModel.UserId;
            if (operatorModel.UserName == "admin")
            {
                salerID = new Guid();
            }
            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<Customer> cusList = _customerApp.GetList(name, salerID, 1).ToPagedList(page, 10);
            return View(cusList);
        }

        /// <summary>
        /// 客户信息
        /// </summary>
        /// <param name="id"></param>
        public ActionResult Info()
        {
            string cid = Request.QueryString["CID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (cus != null)
            {
                ViewBag.Customer = cus;
            }
            return View();
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            //ViewBag.SalerList = _customerApp.GetUserList(salersID);
            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 添加客户[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(FormCollection fc)
        {
            Customer cus = new Customer();
            cus.Id = Guid.NewGuid();
            cus.Name = fc["Name"];
            cus.ShortName = fc["ShortName"];

            //cus.UserId = new Guid(fc["Saler"]);
            //cus.UserName = fc["SalerName"];
            cus.UserId = operatorModel.UserId;
            cus.UserName = operatorModel.FamilyName;

            cus.Address = fc["Address"];
            cus.Corporation = fc["Corporation"];
            cus.Product = fc["Products"];
            cus.Yield = Convert.ToDecimal(fc["Yield"]);
            cus.Range = fc["Range"];
            cus.AccountPeriod = fc["AccountPeriod"];
            cus.BillingFlow = fc["BillingFlow"];
            cus.StatementDate = fc["StatementDate"];
            cus.StatementCount = fc["StatementCount"];
            cus.Memo = fc["Memo"];
            cus.CreateTime = DateTime.Now;
            cus.CreatorId = operatorModel.UserId;
            cus.CreatorName = operatorModel.FamilyName;
            cus.Principal = fc["Data"];
            cus.Type = 1;
            cus.Market = 1;
            cus.Status = 1;

            int i = _customerApp.Insert(cus);
            if (i == 1)
            {
                return RedirectToAction("Index", "Customer");
            }
            else
            {
                ViewBag.Info = "抱歉，操作失败！";
            }

            return View();
        }

        /// <summary>
        /// 编辑客户
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            //ViewBag.SalerList = _customerApp.GetUserList(salersID);
            ViewBag.RealName = operatorModel.FamilyName;
            string cid = Request.QueryString["CID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (cus != null)
            {
                ViewBag.Customer = cus;
            }
            return View();
        }

        /// <summary>
        /// 编辑客户[提交数据]
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(FormCollection fc)
        {
            string cid = fc["CustomerID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (null != cus)
            {
                cus.Name = fc["Name"];
                cus.ShortName = fc["ShortName"];
                //cus.UserId = new Guid(fc["Saler"]);
                cus.Address = fc["Address"];
                cus.Corporation = fc["Corporation"];
                cus.Product = fc["Products"];
                cus.Yield = Convert.ToDecimal(fc["Yield"]);
                cus.Range = fc["Range"];
                cus.AccountPeriod = fc["AccountPeriod"];
                cus.BillingFlow = fc["BillingFlow"];
                cus.StatementDate = fc["StatementDate"];
                cus.StatementCount = fc["StatementCount"];
                cus.Memo = fc["Memo"];
                cus.Status = Convert.ToInt32(fc["Status"]);
                cus.Principal = fc["Data"];
                cus.Type = 1;
                cus.Market = 1;

                int i = _customerApp.Update(cus);
                if (i == 1)
                {
                    return RedirectToAction("Index", "Customer");
                }
                else
                {
                    ViewBag.Info = "抱歉，操作失败！";
                }
            }

            return View();
        }

        /// <summary>
        /// 客户已关联的产品列表
        /// </summary>
        /// <returns></returns>
        public JsonResult CusProductList(string id)
        {
            Guid cid = new Guid(id);
            List<ProductView> cusProList = _customerProductApp.GetCustomerProList(cid);
            return Json(cusProList);
        }

        /// <summary>
        /// 客户关联产品
        /// </summary>
        /// <returns></returns>
        public ActionResult Related()
        {
            string str = Request.QueryString["CID"];
            Guid id = new Guid(str);
            Customer cus = _customerApp.GetInfo(id);
            List<ProductView> ls = _customerProductApp.GetList(id);

            ViewBag.Customer = cus;
            ViewBag.ProList = ls;
            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 提交客户关联产品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Related(FormCollection fc)
        {
            string proList = fc["ProData"];
            Guid cusID = new Guid(fc["CustomerID"]);
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<CustomerProduct> ls = js.Deserialize<List<CustomerProduct>>(proList);
            foreach (var obj in ls)
            {
                obj.Id = Guid.NewGuid();
            }

            int resDel = _customerProductApp.DeleteRelated(cusID);
            int res = _customerProductApp.AddList(ls);
            if (res >= 0)
            {
                return RedirectToAction("Index", "Customer");
            }

            return View();
        }

        /// <summary>
        /// 售后查询客户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult SaleCusList(int page = 1, string name = "")
        {
            ViewBag.Name = name;
            ViewBag.SalerList = _customerApp.GetUserList(salersID);

            IPagedList<Customer> cusList = _customerApp.GetList(name, new Guid(), 1).ToPagedList(page, 10);
            return View(cusList);
        }
    }
}